package com.purplesoft.zettl.Utilities;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.widget.TextView;

import com.purplesoft.zettl.R;
import com.purplesoft.zettl.Database.DatabaseExecuter;
import com.purplesoft.zettl.DatabaseTables.TblCart;
import com.purplesoft.zettl.DatabaseViews.VwCartItems;
import com.purplesoft.zettl.DatabaseViews.VwPurchaseItems;

public class CalculateTotalsCart {
	
	private Cursor csVwPurchaseItems, csVwCartItems, csTblCart;
	private DatabaseExecuter dbExec;
	private SharedPrefs sharedPrefs;
	
	public void calculateTotals(Context context) {
		
		Activity activity = (context instanceof Activity) ? (Activity) context : null;
		TextView tvAmountItems = (TextView) activity.findViewById(R.id.tvAmountItems_Cart);
		TextView tvAmountSumList = (TextView) activity.findViewById(R.id.tvAmountTotalList_Cart);
		TextView tvAmountSumCart = (TextView) activity.findViewById(R.id.tvAmountTotalCart_Cart);
		TextView tvAmountSumTotal = (TextView) activity.findViewById(R.id.tvAmountTotal_Cart);
		
		dbExec = new DatabaseExecuter(context);
		sharedPrefs = new SharedPrefs(context);
		
		double totalList = 0;
		double totalCart = 0;
		double amount = 0;
		double price = 0;
		double pricePer = 0;
//		csVwCartItems = context.getContentResolver().query(DatabaseContentProvider.VWCartItems, null, null, null, null);
		csVwCartItems = dbExec.queryFromVwCartItems(VwCartItems.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), -99));
		csVwCartItems.moveToFirst();
		for (int i = 0; i < csVwCartItems.getCount(); i++) {
			amount = csVwCartItems.getDouble(csVwCartItems.getColumnIndex(VwCartItems.Columns.Amount));
			price = csVwCartItems.getDouble(csVwCartItems.getColumnIndex(VwCartItems.Columns.Price));
			pricePer = csVwCartItems.getDouble(csVwCartItems.getColumnIndex(VwCartItems.Columns.PricePer));
			totalCart = totalCart + ((amount * price) / pricePer);
			if (i < csVwCartItems.getCount()) csVwCartItems.moveToNext();
		}
//		csVwPurchaseItems = context.getContentResolver().query(DatabaseContentProvider.VWPurchaseItems, null, null,	null, null);
		csVwPurchaseItems = dbExec.queryFromVwPurchaseItems(VwPurchaseItems.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), -99));
		csVwPurchaseItems.moveToFirst();
		for (int i = 0; i < csVwPurchaseItems.getCount(); i++) {
			amount = csVwPurchaseItems.getDouble(csVwPurchaseItems.getColumnIndex(VwCartItems.Columns.Amount));
			price = csVwPurchaseItems.getDouble(csVwPurchaseItems.getColumnIndex(VwCartItems.Columns.Price));
			pricePer = csVwPurchaseItems.getDouble(csVwPurchaseItems.getColumnIndex(VwCartItems.Columns.PricePer));
			totalList = totalList + ((amount * price) / pricePer);
			if (i < csVwPurchaseItems.getCount()) csVwPurchaseItems.moveToNext();
		}
//		csTblCart = context.getContentResolver().query(DatabaseContentProvider.TBLCart, null, null,	null, null);
		csTblCart = dbExec.queryFromTblCart(TblCart.Columns.ListID, sharedPrefs.getInt(context.getResources().getString(R.string.zettl_current_list_id), -99));
		int purchaseItemCount = csVwPurchaseItems.getCount();
		int cartCount = csTblCart.getCount();
		tvAmountItems.setText(cartCount + "/" + (cartCount + purchaseItemCount));
		tvAmountSumList.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(totalList)));
		tvAmountSumCart.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(totalCart)));
		tvAmountSumTotal.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(totalCart + totalList)));
		csVwCartItems.close();
		csVwPurchaseItems.close();
		csTblCart.close();

	}

}
